"""视频相关路由"""
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from typing import Optional
from app.config.database import get_db
from app.controllers.video_controller import video_controller
from app.middlewares.auth import get_optional_current_user
from app.schemas.common import ResponseModel, PageResponse
from app.schemas.video import VideoDetailResponse

router = APIRouter(prefix="/api/videos", tags=["videos"])


@router.get("/recommended", response_model=ResponseModel[PageResponse])
def get_recommended_videos(
    page: int = Query(1, ge=1, description="页码"),
    pageSize: int = Query(10, ge=1, le=50, description="每页数量"),
    db: Session = Depends(get_db)
):
    """获取推荐视频列表"""
    try:
        result = video_controller.get_recommended_videos(db, page, pageSize)
        return ResponseModel(
            code=200,
            message="success",
            data=result
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/hot", response_model=ResponseModel[PageResponse])
def get_hot_videos(
    page: int = Query(1, ge=1, description="页码"),
    pageSize: int = Query(10, ge=1, le=50, description="每页数量"),
    db: Session = Depends(get_db)
):
    """获取热门视频列表"""
    try:
        result = video_controller.get_hot_videos(db, page, pageSize)
        return ResponseModel(
            code=200,
            message="success",
            data=result
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/latest", response_model=ResponseModel[PageResponse])
def get_latest_videos(
    page: int = Query(1, ge=1, description="页码"),
    pageSize: int = Query(10, ge=1, le=50, description="每页数量"),
    db: Session = Depends(get_db)
):
    """获取最新视频列表"""
    try:
        result = video_controller.get_latest_videos(db, page, pageSize)
        return ResponseModel(
            code=200,
            message="success",
            data=result
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/slide", response_model=ResponseModel)
def get_slide_videos(db: Session = Depends(get_db)):
    """获取幻灯片视频（前5个热门视频）"""
    try:
        videos = video_controller.get_slide_videos(db)
        return ResponseModel(
            code=200,
            message="success",
            data=videos
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/category/{category_id}", response_model=ResponseModel[PageResponse])
def get_category_videos(
    category_id: int,
    page: int = Query(1, ge=1, description="页码"),
    pageSize: int = Query(10, ge=1, le=50, description="每页数量"),
    db: Session = Depends(get_db)
):
    """获取分类视频列表"""
    try:
        result = video_controller.get_category_videos(db, category_id, page, pageSize)
        return ResponseModel(
            code=200,
            message="success",
            data=result
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/{video_id}", response_model=ResponseModel[dict])
def get_video_detail(
    video_id: int,
    db: Session = Depends(get_db),
    current_user_id: Optional[int] = Depends(get_optional_current_user)
):
    """获取视频详情"""
    try:
        video_detail = video_controller.get_video_detail(db, video_id, current_user_id)
        if not video_detail:
            raise HTTPException(status_code=404, detail="视频不存在")
        return ResponseModel(
            code=200,
            message="success",
            data=video_detail
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
